package com.shawn.jetpackstady.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.shawn.jetpackstady.R;

public abstract class BaseFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = setView(inflater, container);
        //监听back必须设置的
        view.setFocusable(true);
        view.setFocusableInTouchMode(true);
        //然后在写这个监听器
        view.setOnKeyListener(backlistener);
        return view;
    }

    public abstract View setView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container);

    /**
     * 拦截back事件
     * 1.直接返回上层界面
     */
    private View.OnKeyListener backlistener = (view, i, keyEvent) -> {
        if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) {
            //这边判断,如果是back的按键被点击了   就自己拦截实现掉
            if (i == KeyEvent.KEYCODE_BACK) {
                getActivity().finish();
                return true;//表示处理了
            }
        }
        return false;
    };
}
